package cn.txy.param.controller;

import cn.txy.config.R;
import cn.txy.param.domain.Param;
import cn.txy.param.query.ParamQuery;
import cn.txy.param.service.ParamService;
import cn.txy.param.service.impl.ParamServiceImpl;
import cn.txy.system.annotation.PreAuthorize;
import cn.txy.system.domain.Logininfo;
import cn.txy.system.service.LogininfoService;
import cn.txy.system.service.impl.LogininfoServiceImpl;
import cn.txy.system.vo.LogininfoVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/param")
public class ParamController {
    @Autowired
    private ParamServiceImpl paramService;
    @Autowired
    private LogininfoServiceImpl logininfoService;

    @GetMapping("/{id}")
    @PreAuthorize(name = "根据ID查询参数",sn="param:getOne",descs = "根据ID查询参数")
    public R findById(@PathVariable("id")Long id){
        return R.ok(paramService.findById(id));
    }

    @GetMapping
    @PreAuthorize(name = "查询参数",sn="param:getAll",descs = "查询参数")
    public R findAll(){
        return R.ok(paramService.findAll());
    }

    @DeleteMapping("/{id}")
    @PreAuthorize(name = "根据ID删除参数",sn="param:delOne",descs = "根据ID删除参数")
    public R deleteById(@PathVariable("id")Long id){
        paramService.deleteById(id);
        return R.ok();
    }
    @PostMapping("/deleteAll")
    @PreAuthorize(name = "删除参数",sn="param:delAll",descs = "删除参数")
    public R batchDelete(@RequestBody List<Long> ids){
        paramService.batchDelete(ids);
        return R.ok();
    }

    @ApiOperation("添加和修改")
    @PostMapping
    @PreAuthorize(name = "添加和修改参数",sn="param:saveOne",descs = "添加和修改参数")
    public R add(@RequestBody Param param){
        if(param.getId() == null){
            param.setCreateTime(new Date());
//            param.setOperatorName();//截取header下面logininfo里面的username
            paramService.add(param);
        }else {
            param.setUpdateTime(new Date());
//            param.setOperatorName();//截取header下面logininfo里面的username
            paramService.update(param);
        }
        return R.ok();
    }
    @PostMapping("/page")
    @PreAuthorize(name = "分页查询参数",sn="param:page",descs = "分页查询参数")
    public R getPage(@RequestBody ParamQuery paramQuery){
        PageHelper.startPage(paramQuery.getPage(),paramQuery.getSize());
        List<Param> params = paramService.queryPage(paramQuery);
        PageInfo<Param> pageInfo = new PageInfo<>(params);
        return R.ok(pageInfo);
    }
}
